package com.example.cloud.alibaba.seata.business.service;


import com.example.cloud.alibaba.seata.common.dto.BusinessDTO;
import com.example.cloud.alibaba.seata.common.dto.CommodityDTO;
import com.example.cloud.alibaba.seata.common.dto.OrderDTO;
import com.example.cloud.alibaba.seata.common.exception.CustomException;
import com.example.cloud.alibaba.seata.common.feign.IOrderService;
import com.example.cloud.alibaba.seata.common.feign.IStorageService;
import com.example.cloud.alibaba.seata.common.utils.SimpleMessage;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: lidong
 * @Description  Dubbo业务发起方逻辑
 * @Date Created in 2019/9/5 18:36
 */
@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService{

    @Autowired
    private IStorageService storageService;

    @Autowired
    private IOrderService orderService;

    boolean flag;

    /**
     * 处理业务逻辑 正常的业务逻辑
     * @Param:
     * @Return:
     */
    @GlobalTransactional(timeoutMills = 300000, rollbackFor = Exception.class,name = "openFeign-gts-seata-example")
    @Override
    public SimpleMessage handleBusiness(BusinessDTO businessDTO) {
       // log.info("开始全局事务，XID = " + RootContext.getXID());
        //1、扣减库存
        CommodityDTO commodityDTO = new CommodityDTO();
        commodityDTO.setCommodityCode(businessDTO.getCommodityCode());
        commodityDTO.setCount(businessDTO.getCount());
        SimpleMessage storageResponse = storageService.decreaseStorage(commodityDTO);
        //2、创建订单
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setUserId(businessDTO.getUserId());
        orderDTO.setCommodityCode(businessDTO.getCommodityCode());
        orderDTO.setOrderCount(businessDTO.getCount());
        orderDTO.setOrderAmount(businessDTO.getAmount());
        SimpleMessage<OrderDTO> response = orderService.createOrder(orderDTO);

        if (storageResponse.getCode() != 200 || response.getCode() != 200) {
            throw new CustomException("处理业务报错！！！");
        }

        return SimpleMessage.info("处理业务成功~");
    }

    /**
     * 出处理业务服务，出现异常回顾
     *
     * @param businessDTO
     * @return
     */
    @GlobalTransactional(timeoutMills = 300000, name = "dubbo-gts-seata-example")
    @Override
    public SimpleMessage handleBusiness2(BusinessDTO businessDTO) {
       // log.info("开始全局事务，XID = " + RootContext.getXID());
        //1、扣减库存
        CommodityDTO commodityDTO = new CommodityDTO();
        commodityDTO.setCommodityCode(businessDTO.getCommodityCode());
        commodityDTO.setCount(businessDTO.getCount());
        SimpleMessage storageResponse = storageService.decreaseStorage(commodityDTO);
        //2、创建订单
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setUserId(businessDTO.getUserId());
        orderDTO.setCommodityCode(businessDTO.getCommodityCode());
        orderDTO.setOrderCount(businessDTO.getCount());
        orderDTO.setOrderAmount(businessDTO.getAmount());
        SimpleMessage<OrderDTO> response = orderService.createOrder(orderDTO);

//        打开注释测试事务发生异常后，全局回滚功能
        if (!flag) {
            throw new RuntimeException("测试抛异常后，分布式事务回滚！");
        }

        if (storageResponse.getCode() != 200 || response.getCode() != 200) {
            throw new CustomException("处理业务2失败！！！");
        }

        return SimpleMessage.info("处理业务2成功~");
    }
}
